home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
PixelPlotTest
/
PixelPlotTest.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-13
|
5KB
|
181 lines
// PixelPlotTest
// By Ingemar Ragnemalm 1994-1995
// This demo demonstrates the difference in speed when plotting single pixels
// in different ways. The conclusion is that the fastest method is over 300
// times faster than the toolbox call SetCPixel
#define kSizeH 300
#define kSizeV 40
/* Standard inits */
static void InitToolbox(void)
{
InitGraf (&qd.thePort);
InitFonts ();
FlushEvents (everyEvent,0);
InitWindows ();
InitMenus ();
TEInit ();
InitDialogs (nil);
InitCursor ();
} /*InitToolbox*/
void main(void)
{
WindowPtr w;
RGBColor c, saveColor;
long startTime, endTime;
short h, v, i;
Rect r;
long rowBytes;
Ptr pixAddr;
long baseAddr;
Point origin;
long rowStartTable[kSizeV+1];
SignedByte mmuMode;
Str255 tempStr;
InitToolbox();
SetRect(&r, 100, 100, 400, 301);
w = NewCWindow(nil, &r, "\pPixel Plotting Speed Test", true, 2, (WindowPtr)-1, false, 0);
SetPort(w);
GetForeColor(&saveColor);
c.red = 0xffff;
c.green = 0;
c.blue = 0xffff;
startTime = TickCount ();
for ( h = 0 ; h <= kSizeH ;h++)
for ( v = 0 ; v <= kSizeV ;v++)
SetCPixel(h, v, &c);
endTime = TickCount ();
// Report the result
MoveTo(10, kSizeV + 20);
NumToString(endTime - startTime, tempStr);
DrawString("\pSetCPixel test: ");
DrawString(tempStr);
DrawString("\p ticks.");
c.red = 0xffff;
c.green = 0xffff;
c.blue = 0;
RGBForeColor(&c);
startTime = TickCount ();
for ( h = 0 ; h <= kSizeH ;h++)
for ( v = 0 ; v <= kSizeV ;v++)
{
// RGBForeColor(c); // You can uncomment this line as a variant
MoveTo(h, v);
Line(0, 0);
};
endTime = TickCount ();
RGBForeColor(&saveColor);
// Report the result
MoveTo(10, kSizeV + 40);
NumToString(endTime - startTime, tempStr);
DrawString("\pMoveTo/LineTo test: ");
DrawString(tempStr);
DrawString("\p ticks.");
c.red = 0;
c.green = 0xffff;
c.blue = 0xffff;
RGBForeColor(&c);
startTime = TickCount ();
for ( h = 0 ; h <= kSizeH ;h++)
for ( v = 0 ; v <= kSizeV ;v++)
{
// RGBForeColor(c); // You can uncomment this line as a variant
SetRect(&r, h, v, h + 1, v + 1);
PaintRect(&r);
};
endTime = TickCount ();
RGBForeColor(&saveColor);
// Report the result
MoveTo(10, kSizeV + 60);
NumToString(endTime - startTime, tempStr);
DrawString("\pSetRect/PaintRect test: ");
DrawString(tempStr);
DrawString("\p ticks.");
if ( (**(**GetMainDevice()).gdPMap).pixelSize != 8 )
{
MoveTo(10, kSizeV + 80);
DrawString("\pDirect plotting is only supported in 256 colors/grays.");
}
else
{
mmuMode = true32b;
rowBytes = ((**(**GetMainDevice()).gdPMap).rowBytes & 0x3fff);
SetRect(&r, 0, 0, kSizeH, kSizeV);
ShieldCursor(&r, *(Point *)&(**((CWindowPtr)w)->portPixMap).bounds);
baseAddr = (long)(**(**GetMainDevice()).gdPMap).baseAddr;
/* Find the offset to the window - though I really know it is 100,100! */
SetPt(&origin, 0, 0);
LocalToGlobal(&origin);
startTime = TickCount ();
SwapMMUMode(&mmuMode);
for ( i = 1 ; i <= 100 ;i++)
for ( h = 0 ; h <= kSizeH ;h++)
for ( v = 0 ; v <= kSizeV ;v++)
{
pixAddr = (Ptr) (baseAddr + rowBytes * (v + origin.v) + h + origin.h);
*pixAddr = 0x70 + i; /*Some pixel value. We can get this through the color table.*/
};
SwapMMUMode(&mmuMode);
endTime = TickCount ();
ShowCursor ();
// Report the result
MoveTo(10, kSizeV + 80);
NumToString(endTime - startTime, tempStr);
DrawString("\pDirect plotting test: ");
DrawString(tempStr);
DrawString("\p ticks/100 times.");
/*Optimize by precalculating as much as possible:*/
for ( v = 0 ; v <= kSizeV ; v++)
rowStartTable[v] = baseAddr + rowBytes * (v + origin.v) + origin.h;
ShieldCursor(&r, *(Point *)&(**((CWindowPtr)w)->portPixMap).bounds);
startTime = TickCount ();
SwapMMUMode(&mmuMode); /*We must be in 32-bit mode*/
for ( i = 1 ; i <= 100 ;i++)
for ( h = 0 ; h <= kSizeH ;h++)
for ( v = 0 ; v <= kSizeV ;v++)
{
pixAddr = (Ptr)(rowStartTable[v] + h);
*pixAddr = 0x60 + i; /*Some pixel value. We can get this through the color table.*/
};
SwapMMUMode(&mmuMode); /*Back to previous addressing mode*/
endTime = TickCount ();
ShowCursor (); /*Balances ShieldCursor*/
// Report the result
MoveTo(10, kSizeV + 100);
NumToString(endTime - startTime, tempStr);
DrawString("\pOpt. direct plotting test: ");
DrawString(tempStr);
DrawString("\p ticks/100 times.");
};
MoveTo(10, kSizeV + 120);
DrawString("\pClick mouse to exit.");
while ( ! Button () )
;
} /*main*/